package com.pro.common.service.message.service;

import com.pro.common.api.CommonConst;
import com.pro.framework.api.enums.EnumEnv;
import com.pro.common.api.message.model.db.SysMsgRecord;
import com.pro.common.service.message.dao.SysMsgRecordDao;
import com.pro.common.service.properties.BaseProperties;
import com.pro.framework.api.cache.ICacheManagerCenter;
import com.pro.framework.mybatisplus.BaseService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 短信发送记录 服务实现类
 */
@Service
@AllArgsConstructor
public class SysMsgRecordService extends BaseService<SysMsgRecordDao, SysMsgRecord> {
    private BaseProperties baseProperties;
    private ICacheManagerCenter cacheManagerCenter;

    public String getCode(String account) {
        return cacheManagerCenter.get(CommonConst.CacheKey.CHECK_CODE, account, String.class);
    }

    public void setCode(String account, String smsCode) {
        cacheManagerCenter.put(CommonConst.CacheKey.CHECK_CODE, account, smsCode);
    }

    public boolean checkCode(String phone, String smsCode) {
        if (!EnumEnv.prod.equals(baseProperties.getCommonEnv()) || phone.startsWith("13800")) {
            return true;
        }
        String attribute = getCode(phone);
        if (attribute == null) {
            return false;
        }
        return attribute.equals(smsCode);
    }
}
